package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.DestHotel;
import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.IDestHotelService;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IHotelScoreService;
import cn.wolfcode.luowowo.article.service.IHotelTagService;
import cn.wolfcode.luowowo.common.query.HotelQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.Reader;
import java.util.Collections;
import java.util.List;



import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.Reader;

import java.net.URL;

import java.nio.charset.Charset;

import org.json.JSONException;

import org.json.JSONObject;

import java.io.PrintWriter;

import java.net.URLConnection;
@Controller
@RequestMapping("/hotel")
public class HotelController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IHotelTagService hotelTagService;
    @Reference
    private IHotelScoreService hotelScoreService;
    @Reference
    private IDestHotelService destHotelService;


    @RequestMapping("hotel")
    public String hotel(Model model){
        //国内,dest,12个
        List<DestHotel> dest = destHotelService.listByType(1);
        model.addAttribute("dest",dest);
        //海外 overseas,
        List<DestHotel> overseas = destHotelService.listByType(2);
        model.addAttribute("overseas",overseas);
        //hotelTags
        model.addAttribute("hotelTags",hotelTagService.listAll());
        //特价酒店
        //hotelCity,查询有特价酒店的城市
        List<Destination> l = hotelScoreService.getHotelCitys();
        model.addAttribute("hotelCity",hotelScoreService.getHotelCitys());
        return "hotel/hotel";
    }


    @RequestMapping("theme")

    public String theme(String name,Model model){
        //根据主题名,查询所有6个目的地
        List<Destination> list = destinationService.listByHotelTheme6(name);
        model.addAttribute("list",list);
        return "hotel/hotelTpl";
    }

    @RequestMapping("theme1")
    public String theme1(String cityName,Model model ){
        //hotelScore
        model.addAttribute("hotelScore",hotelScoreService.listByDestId8(cityName));
        return "hotel/hotelTpl1";
    }


    /**
     * 根据目的地获取酒店信息
     * @return
     */
    @RequestMapping("h")
    public String searchHotel(@ModelAttribute("qo")HotelQueryObject qo, Model model){
        model.addAttribute("destId",617);
        return "hotel/dingjiudian";
    }

    @RequestMapping("list")
    @ResponseBody
    public Object list(@ModelAttribute("qo") HotelQueryObject qo){
        AjaxResult result = new AjaxResult();

        String checkIn = "2019-10-26";
        String checkOut = "2019-10-30";
        String destId = "617";
        if (qo.getCheckIn()!=null){
            checkIn=qo.getCheckIn();
        }
        if (qo.getCheckOut()!=null){
            checkOut=qo.getCheckOut();
        }
        if (qo.getDestId()!=null){
            destId=qo.getDestId();
        }

        // 请求示例 url 默认请求参数已经做URL编码

        String url = "http://api01.6bqb.com/ctrip/search?apikey=B6B47D314F0AF18F116326620CE13204&cityId="+destId+"&page=1&checkIn="+checkIn+"&checkOut="+checkOut+"&orderBy=99&orderType=1&roomNumber=1&adult=1&children=0";

        JSONObject json = null;
        try {
            json = getRequestFromUrl(url);
            result.setData(json.toString());
        } catch (IOException e) {
            e.printStackTrace();
            result.mark("操作失败");
        }

        System.err.println(json.toString());

        return result;
    }




    @RequestMapping("{destId}")
    public String searchHotel2(@PathVariable String destId,Model model){
        model.addAttribute("destId",destId);
        return "hotel/dingjiudian";
    }





    private  String readAll(Reader rd) throws IOException {

        StringBuilder sb = new StringBuilder();

        int cp;

        while ((cp = rd.read()) != -1) {

            sb.append((char) cp);

        }

        return sb.toString();

    }

    private JSONObject postRequestFromUrl(String url, String body) throws IOException, JSONException {

        URL realUrl = new URL(url);

        URLConnection conn = realUrl.openConnection();

        conn.setDoOutput(true);

        conn.setDoInput(true);

        PrintWriter out = new PrintWriter(conn.getOutputStream());

        out.print(body);

        out.flush();

        InputStream instream = conn.getInputStream();

        try {

            BufferedReader rd = new BufferedReader(new InputStreamReader(instream, Charset.forName("UTF-8")));

            String jsonText = readAll(rd);

            JSONObject json = new JSONObject(jsonText);

            return json;

        } finally {

            instream.close();

        }

    }

    private   JSONObject getRequestFromUrl(String url) throws IOException, JSONException {

        URL realUrl = new URL(url);

        URLConnection conn = realUrl.openConnection();

        InputStream instream = conn.getInputStream();

        try {

            BufferedReader rd = new BufferedReader(new InputStreamReader(instream, Charset.forName("UTF-8")));

            String jsonText = readAll(rd);

            JSONObject json = new JSONObject(jsonText);

            return json;

        } finally {

            instream.close();

        }

    }

}
